setwd("/Users/leonardocarella/Documents/LSQ_27092023") # change to local wd
`%notin%` <- Negate(`%in%`)
library(tidyverse)
library(readxl)
library(lubridate)
library(lmtest)
library(multiwayvcov)
library(stargazer)
library(sandwich)
library(dfidx)
library(mlogit)
library(DescTools)
library(margins)

# This file computes SMD and list prospective candidacy success,
# subsequently employed to derive continuous candidacy quality (re-election probability)
# from data on election results in SMD and list elections 

#### Get FPTP election margins at t and result at t+1 ####

# Actual 1972 fptp_results (all districts correspond to 1976 districts)
fptp_results72 <- read_excel("btw72_kerg.xlsx") %>% 
  dplyr::filter(!is.na(WahlkreisNr.) & WahlkreisNr. < 900) %>%
  dplyr::rename(voters_first = `GültigeErststimmen`,
                voters_second = `GültigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = `CDUErststimmen`/voters_first,
                SPD_first = `SPDErststimmen`/voters_first,
                FDP_first = `F.D.P.Erststimmen`/voters_first,
                CSU_first = `CSUErststimmen`/voters_first) %>%
  dplyr::select(Land,`WahlkreisNr.`,Name, CDU_first, SPD_first, 
                FDP_first, CSU_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin), names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")



outcome76 <- rio::import("btw1976bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1) 

fptp_results72$winner_1976 <- NA

for (i in unique(fptp_results72$WahlkreisNr.)) {
  fptp_results72$winner_1976[fptp_results72$WahlkreisNr. == i] <- 
    outcome76$`Partei/\r\nListenvereinigung`[outcome76$Wkr == i]
}

fptp_results72$win_t_plus_1 <- ifelse(fptp_results72$party == fptp_results72$winner_1976,1,0)

# Actual 1976 fptp_results (only some correspond to 1980 districts)
fptp_results76 <- read_excel("btw76_kerg.xlsx") %>% 
  dplyr::filter(!is.na(WahlkreisNr.) & WahlkreisNr. < 900) %>%
  dplyr::rename(voters_first = `G¸ltigeErststimmen`,
                voters_second = `G¸ltigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = `C D UErststimmen`/voters_first,
                SPD_first = `S P DErststimmen`/voters_first,
                FDP_first = `F.D.P.Erststimmen`/voters_first,
                CSU_first = `C S UErststimmen`/voters_first) %>%
  dplyr::select(Land,`WahlkreisNr.`,Name, CDU_first, SPD_first, 
                FDP_first, CSU_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin), names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")

#exclude redistricted constituencies
redist <- rio::import("deu_1976_1980_correspondence.xlsx")
fptp_results76 <- fptp_results76 %>% 
  dplyr::filter(`WahlkreisNr.` %in% c(redist$wkr_no_1976[redist$unchanged_wk == 1]))
for (i in unique(fptp_results76$`WahlkreisNr.`)) {
  fptp_results76$wk_1980[fptp_results76$`WahlkreisNr.` == i] <- redist$wk_no_1980[redist$wkr_no_1976 == i]
}

outcome80 <- rio::import("btw1980_bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1)

fptp_results76$winner_1980 <- NA

for (i in unique(fptp_results76$wk_1980)) {
  fptp_results76$winner_1980[fptp_results76$wk_1980 == i] <- 
    outcome80$`Partei/\r\nListenvereinigung`[outcome80$Wkr == i]
}

fptp_results76$win_t_plus_1 <- ifelse(fptp_results76$party == fptp_results76$winner_1980,1,0)

# Actual 1980 fptp_results (all correspond to 1983 districts)

fptp_results80 <- read_excel("btw80_kerg.xlsx") %>% 
  dplyr::filter(!is.na(WahlkreisNr.) & WahlkreisNr. < 900) %>%
  dplyr::rename(voters_first = `G¸ltigeErststimmen`,
                voters_second = `G¸ltigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = `C D UErststimmen`/voters_first,
                SPD_first = `S P DErststimmen`/voters_first,
                FDP_first = `F.D.P.Erststimmen`/voters_first,
                GRU_first = GRUENEErststimmen/voters_first,
                CSU_first = `C S UErststimmen`/voters_first) %>%
  dplyr::select(Land,`WahlkreisNr.`,Name, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")

outcome83 <- rio::import("btw1983bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1)

fptp_results80$winner_1983 <- NA
for (i in unique(fptp_results80$WahlkreisNr.)) {
  fptp_results80$winner_1983[fptp_results80$WahlkreisNr. == i] <- 
    outcome83$`Partei/\r\nListenvereinigung`[outcome83$Wkr == i]
}

fptp_results80$win_t_plus_1 <- ifelse(fptp_results80$party == fptp_results80$winner_1983,1,0)

# Actual 1983 fptp_results (all correspond to 1987 districts)

fptp_results83 <- read_excel("btw83_kerg.xlsx") %>% 
  dplyr::filter(!is.na(WahlkreisNr.) & WahlkreisNr. < 900) %>%
  dplyr::rename(voters_first = `GültigeErststimmen`,
                voters_second = `GültigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = `F.D.P.Erststimmen`/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GRUENEErststimmen`/voters_first) %>%
  dplyr::select(Land,`WahlkreisNr.`,Name, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")


outcome87 <- rio::import("btw1987bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1)

fptp_results83$winner_1987 <- NA
for (i in unique(fptp_results83$WahlkreisNr.)) {
  fptp_results83$winner_1987[fptp_results83$WahlkreisNr. == i] <- 
    outcome87$`Partei/\r\nListenvereinigung`[outcome87$Wkr == i]
}

fptp_results83$win_t_plus_1 <- ifelse(fptp_results83$party == fptp_results83$winner_1987,1,0)

# Actual 1987 fptp_results (only Bavaria redistricted in 1990)

fptp_results87 <- read_excel("btw87_kerg.xlsx") %>% 
  dplyr::filter(!is.na(WahlkreisNr.) & WahlkreisNr. < 900)%>%
  dplyr::rename(voters_first = `GültigeErststimmen`,
                voters_second = `GültigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = `F.D.P.Erststimmen`/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GRUENEErststimmen`/voters_first,
                NPD_first = NPDErststimmen/voters_first) %>%
  dplyr::select(Land,`WahlkreisNr.`, Name, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, NPD_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, NPD_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, NPD_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, NPD_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, NPD_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, NPD_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, NPD_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")%>%
  dplyr::filter(Land != "BY")

outcome90 <- rio::import("btw1990bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1) %>%
  dplyr::filter(Wkr %in% unique(fptp_results87$WahlkreisNr.))

fptp_results87$winner_1990 <- NA
for (i in unique(fptp_results87$WahlkreisNr.)) {
  fptp_results87$winner_1990[fptp_results87$WahlkreisNr. == i] <- 
    outcome90$`Partei/\r\nListenvereinigung`[outcome90$Wkr == i]
}

fptp_results87$win_t_plus_1 <- ifelse(fptp_results87$party == fptp_results87$winner_1990,1,0)

# Actual 1990 fptp_results (Berlin redistricted in 1994)

fptp_results90 <- read_excel("btw90_kerg.xlsx") %>% 
  dplyr::filter(as.numeric(WahlkreisNr.) < 400) %>%
  dplyr::rename(voters_first = GültigeErststimmen,
                voters_second = GültigeZweitstimmen) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = FDPErststimmen/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = GRÜNEErststimmen/voters_first,
                PDS_first = PDSErststimmen/voters_first,
                REP_first = REPErststimmen/voters_first,
                B90_first = `B90/GrErststimmen`/voters_first,
                NPD_first = NPDErststimmen/voters_first) %>%
  dplyr::select(`Land`,WahlkreisNr., `Name`, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, PDS_first, REP_first, 
                B90_first, NPD_first)%>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, NPD_first, PDS_first, REP_first, B90_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, NPD_first, PDS_first, REP_first, B90_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, NPD_first, PDS_first, REP_first, B90_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, NPD_first, PDS_first, REP_first, B90_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, NPD_first, PDS_first, REP_first, B90_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, PDS_first, REP_first, B90_first),
                PDS_margin = PDS_first - max(SPD_first, FDP_first, CSU_first, GRU_first, NPD_first, CDU_first, REP_first, B90_first),
                REP_margin = REP_first - max(SPD_first, FDP_first, CSU_first, GRU_first, NPD_first, CDU_first, PDS_first, B90_first),
                B90_margin = B90_first - max(SPD_first, FDP_first, CSU_first, GRU_first, NPD_first, CDU_first, PDS_first, REP_first),) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, NPD_margin, PDS_margin, REP_margin, B90_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")%>%
  dplyr::filter(PDS_first != 0 | party != "PDS")%>%
  dplyr::filter(REP_first != 0 | party != "REP")%>%
  dplyr::filter(B90_first != 0 | party != "B90")%>%
  dplyr::filter(Land != "BE")

outcome94 <- rio::import("btw1994bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1)

fptp_results90$winner_1994 <- NA
for (i in unique(fptp_results90$WahlkreisNr.)) {
  fptp_results90$winner_1994[fptp_results90$WahlkreisNr. == i] <- 
    outcome94$Partei[outcome94$Wkr. == i]
}

fptp_results90$win_t_plus_1 <- ifelse(fptp_results90$party == fptp_results90$winner_1994,1,0)

# Nominal 1994 fptp_results by 1998 constituencies

fptp_results94 <- read_excel("wkumrechnung1998-erst.xlsx", skip = 5) %>%
  dplyr::filter(as.numeric(Wkr) < 329) %>%
  dplyr::rename(voters_first = gültig) %>%
  dplyr::mutate(CDU_first = CDU/voters_first,
                SPD_first = SPD/voters_first,
                FDP_first = `F.D.P.`/voters_first,
                CSU_first = CSU/voters_first,
                GRU_first = GRÜNE/voters_first,
                PDS_first = PDS/voters_first,
                REP_first = REP/voters_first,
                STATT_first = `STATT Partei`/voters_first) %>%
  dplyr::select(`Ld.`,Wkr, `Wkr.-Name`, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, PDS_first, REP_first, STATT_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, STATT_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, STATT_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, PDS_first, REP_first, STATT_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, PDS_first, REP_first, STATT_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, PDS_first, REP_first, STATT_first),
                PDS_margin = PDS_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, REP_first, STATT_first),
                REP_margin = REP_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, CDU_first, STATT_first),
                STATT_margin = STATT_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, CDU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, PDS_margin, REP_margin, STATT_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(PDS_first != 0 | party != "PDS")%>%
  dplyr::filter(REP_first != 0 | party != "REP")%>%
  dplyr::filter(STATT_first != 0 | party != "STATT")


outcome98 <- rio::import("btw1998bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1)

fptp_results94$winner_1998 <- NA
for (i in unique(fptp_results94$Wkr)) {
  fptp_results94$winner_1998[fptp_results94$Wkr == i] <- 
    outcome98$Partei[outcome98$Wkr. == i]
}

fptp_results94$win_t_plus_1 <- ifelse(fptp_results94$party == fptp_results94$winner_1998,1,0)

# Nominal 1998 fptp_results by 2002 constituencies

fptp_results98 <- read_excel("wkumrechnung2002.xlsx", skip = 7) %>%
  dplyr::filter(!is.na(Land)) %>%
  dplyr::rename(voters_first = `GŸltigeErststimmen`,
                voters_second = `GŸltigZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = `F.D.P.Erststimmen`/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GR†NEErststimmen`/voters_first,
                PDS_first = PDSErststimmen/voters_first,
                REP_first = REPErststimmen/voters_first,
                NPD_first = NPDErststimmen/voters_first,
                STATT_first = `STATT ParteiErststimmen`/voters_first) %>%
  dplyr::select(Land,`Wkr-Nr.`, Wahlkreisnamen, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, PDS_first, REP_first, 
                NPD_first, STATT_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, STATT_first, NPD_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, STATT_first, NPD_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, PDS_first, REP_first, STATT_first, NPD_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, PDS_first, REP_first, STATT_first, NPD_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, PDS_first, REP_first, STATT_first, NPD_first),
                PDS_margin = PDS_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, REP_first, STATT_first, NPD_first),
                REP_margin = REP_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, CDU_first, STATT_first, NPD_first),
                STATT_margin = STATT_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, CDU_first, NPD_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, CDU_first, STATT_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, PDS_margin, REP_margin, STATT_margin, NPD_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(PDS_first != 0 | party != "PDS")%>%
  dplyr::filter(REP_first != 0 | party != "REP")%>%
  dplyr::filter(STATT_first != 0 | party != "STATT")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")

outcome02 <- rio::import("btw2002bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 1) %>%
  dplyr::mutate(Partei = recode(Partei, "GRÜNE" = "GRU"))

fptp_results98$winner_2002 <- NA
for (i in unique(fptp_results98$`Wkr-Nr.`)) {
  fptp_results98$winner_2002[fptp_results98$`Wkr-Nr.` == i] <- 
    outcome02$Partei[outcome02$Wkr. == i]
}

fptp_results98$win_t_plus_1 <- ifelse(fptp_results98$party == fptp_results98$winner_2002,1,0)

# Nominal 2002 fptp_results by 2005 constituencies

fptp_results02 <- read_excel("wkumrechnung2005.xlsx", skip = 7) %>%
  dplyr::filter(!is.na(Land)) %>%
  dplyr::rename(voters_first = `GŸltigErststimmen`,
                voters_second = `GŸltigZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = FDPErststimmen/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GR†NEErststimmen`/voters_first,
                PDS_first = PDSErststimmen/voters_first,
                REP_first = REPErststimmen/voters_first,
                NPD_first = NPDErststimmen/voters_first) %>%
  dplyr::select(Land,`Wkr-Nr.`, Wahlkreisnamen, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, PDS_first, REP_first, NPD_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, NPD_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first,  NPD_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, PDS_first, REP_first, NPD_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, PDS_first, REP_first, NPD_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, PDS_first, REP_first, NPD_first),
                PDS_margin = PDS_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, REP_first, NPD_first),
                REP_margin = REP_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, CDU_first, NPD_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, PDS_first, REP_first, CDU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, PDS_margin, REP_margin, NPD_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(PDS_first != 0 | party != "PDS")%>%
  dplyr::filter(REP_first != 0 | party != "REP")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")

outcome05 <- rio::import("btw2005bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 1) %>%
  dplyr::mutate(Partei = recode(Partei, "GRÜNE" = "GRU", "Die Linke." = "PDS"))

fptp_results02$winner_2005 <- NA
for (i in unique(fptp_results02$`Wkr-Nr.`)) {
  fptp_results02$winner_2005[fptp_results02$`Wkr-Nr.` == i] <- 
    outcome05$Partei[outcome05$Wkr. == i]
}

fptp_results02$win_t_plus_1 <- ifelse(fptp_results02$party == fptp_results02$winner_2005,1,0)

# Nominal 2005 fptp_results by 2009 constituencies

fptp_results05 <- read_excel("wkumrechnung2009.xlsx", skip = 7) %>%
  dplyr::filter(`Wkr.-Nr.` < 300) %>%
  dplyr::rename(voters_first = `GŸltigeErststimmen`,
                voters_second = `GŸltigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = FDPErststimmen/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GR†NEErststimmen`/voters_first,
                LIN_first = `Die Linke.Erststimmen`/voters_first,
                REP_first = REPErststimmen/voters_first,
                NPD_first = NPDErststimmen/voters_first) %>%
  dplyr::select(Land,`Wkr.-Nr.`, Wahlkreisname, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first,  NPD_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, LIN_first, REP_first, NPD_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, LIN_first, REP_first, NPD_first),
                LIN_margin = LIN_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, REP_first, NPD_first),
                REP_margin = REP_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, CDU_first, NPD_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, CDU_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, LIN_margin, REP_margin, NPD_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(LIN_first != 0 | party != "LIN")%>%
  dplyr::filter(REP_first != 0 | party != "REP")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")

outcome09 <- rio::import("btw2009bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 1) %>%
  dplyr::mutate(Partei = recode(Partei, "GRÜNE" = "GRU", "DIE LINKE" = "LIN"))

fptp_results05$winner_2009 <- NA
for (i in unique(fptp_results05$`Wkr.-Nr.`)) {
  fptp_results05$winner_2009[fptp_results05$`Wkr.-Nr.` == i] <- 
    outcome09$Partei[outcome09$Wkr. == i]
}

fptp_results05$win_t_plus_1 <- ifelse(fptp_results05$party == fptp_results05$winner_2009,1,0)

# Nominal 2009 fptp_results by 2013 constituencies

fptp_results09 <- read_excel("btwkr13_umrechnung_btw09.xlsx", 
                             skip = 7) %>%
  dplyr::filter(`Wkr.-Nr.` < 300) %>%
  dplyr::rename(voters_first = `GŸltigeErststimmen`,
                voters_second = `GŸltigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = FDPErststimmen/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GR†NEErststimmen`/voters_first,
                LIN_first = `DIE LINKEErststimmen`/voters_first,
                REP_first = REPErststimmen/voters_first,
                PIR_first = PIRATENErststimmen/voters_first, 
                NPD_first = NPDErststimmen/voters_first) %>%
  dplyr::select(Land,`Wkr.-Nr.`, Wahlkreisname, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, LIN_first, REP_first, 
                PIR_first, NPD_first)  %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, LIN_first, REP_first, NPD_first, PIR_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first),
                LIN_margin = LIN_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, REP_first, NPD_first, PIR_first),
                REP_margin = REP_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, CDU_first, NPD_first, PIR_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, CDU_first, PIR_first),
                PIR_margin = PIR_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, CDU_first, NPD_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, LIN_margin, REP_margin, NPD_margin, PIR_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(LIN_first != 0 | party != "LIN")%>%
  dplyr::filter(REP_first != 0 | party != "REP")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")%>%
  dplyr::filter(PIR_first != 0 | party != "PIR")

outcome13 <- rio::import("btw2013bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 1) %>%
  dplyr::mutate(Partei = recode(Partei, "GRÜNE" = "GRU", "DIE LINKE" = "LIN"))

fptp_results09$winner_2013 <- NA
for (i in unique(fptp_results09$`Wkr.-Nr.`)) {
  fptp_results09$winner_2013[fptp_results09$`Wkr.-Nr.` == i] <- 
    outcome13$Partei[outcome13$Wahlkreis == i]
}

fptp_results09$win_t_plus_1 <- ifelse(fptp_results09$party == fptp_results09$winner_2013,1,0)

# Nominal 2013 fptp_results by 2017 constituencies

fptp_results13 <- read_excel("btwkr17_umrechnung_btw13.xlsx", 
                             skip = 7) %>%
  dplyr::filter(`Wkr-Nr.` < 300) %>%
  dplyr::rename(voters_first = `GŸltigeErststimmen`,
                voters_second = `GŸltigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = FDPErststimmen/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GR†NEErststimmen`/voters_first,
                LIN_first = `DIE LINKEErststimmen`/voters_first,
                REP_first = REPErststimmen/voters_first,
                PIR_first = PIRATENErststimmen/voters_first, 
                AFD_first = AfDErststimmen/voters_first,
                NPD_first = NPDErststimmen/voters_first) %>%
  dplyr::select(Land,`Wkr-Nr.`, Wahlkreisname, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, LIN_first, REP_first, 
                PIR_first, AFD_first, NPD_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first, AFD_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first, AFD_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first, AFD_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, LIN_first, REP_first, NPD_first, PIR_first, AFD_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, LIN_first, REP_first, NPD_first, PIR_first, AFD_first),
                LIN_margin = LIN_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, REP_first, NPD_first, PIR_first, AFD_first),
                REP_margin = REP_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, CDU_first, NPD_first, PIR_first, AFD_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, CDU_first, PIR_first, AFD_first),
                AFD_margin = AFD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, CDU_first, PIR_first, NPD_first),
                PIR_margin = PIR_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, REP_first, CDU_first, NPD_first, AFD_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, LIN_margin, REP_margin, NPD_margin, PIR_margin, AFD_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(LIN_first != 0 | party != "LIN")%>%
  dplyr::filter(REP_first != 0 | party != "REP")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")%>%
  dplyr::filter(PIR_first != 0 | party != "PIR")%>%
  dplyr::filter(AFD_first != 0 | party != "AFD")

outcome17 <- read_excel("btw2017_kerg2.xlsx", 
                        skip = 10) %>%
  dplyr::filter(Gebietsart == "Wahlkreis" & Stimme == 1 & 
                  Gruppenart %in% c("Einzelbewerber/Wählergruppe","Partei") & 
                  !is.na(Anzahl)) %>%
  dplyr::mutate(Gebietsnummer = as.numeric(Gebietsnummer)) %>%
  group_by(Gebietsnummer) %>% slice(which.max(Anzahl)) %>%
  dplyr::mutate(Gruppenname = recode(Gruppenname, "GRÜNE" = "GRU", "DIE LINKE" = "LIN", "AfD" = "AFD"))

fptp_results13$winner_2017 <- NA
for (i in unique(fptp_results13$`Wkr-Nr.`)) {
  fptp_results13$winner_2017[fptp_results13$`Wkr-Nr.` == i] <- 
    outcome17$Gruppenname[outcome17$Gebietsnummer == i]
}

fptp_results13$win_t_plus_1 <- ifelse(fptp_results13$party == fptp_results13$winner_2017,1,0)

# Nominal 2017 fptp_results by 2021 constituencies

fptp_results17 <- read_excel("btwkr21_umrechnung_btw17.xlsx")%>%
  dplyr::filter(`Wkr-Nr.` < 300) %>%
  dplyr::rename(voters_first = `GültigeErststimmen`,
                voters_second = `GültigeZweitstimmen`) %>%
  dplyr::mutate(CDU_first = CDUErststimmen/voters_first,
                SPD_first = SPDErststimmen/voters_first,
                FDP_first = FDPErststimmen/voters_first,
                CSU_first = CSUErststimmen/voters_first,
                GRU_first = `GRÜNEErststimmen`/voters_first,
                LIN_first = `DIE LINKEErststimmen`/voters_first,
                PIR_first = PIRATENErststimmen/voters_first, 
                AFD_first = AfDErststimmen/voters_first,
                NPD_first = NPDErststimmen/voters_first) %>%
  dplyr::select(Land,`Wkr-Nr.`, Wahlkreisname, CDU_first, SPD_first, 
                FDP_first, CSU_first, GRU_first, LIN_first, 
                PIR_first, AFD_first, NPD_first) %>%
  rowwise() %>%
  dplyr::mutate(CDU_margin = CDU_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, NPD_first, PIR_first, AFD_first),
                SPD_margin = SPD_first - max(CDU_first, FDP_first, CSU_first, GRU_first, LIN_first, NPD_first, PIR_first, AFD_first),
                FDP_margin = FDP_first - max(SPD_first, CDU_first, CSU_first, GRU_first, LIN_first, NPD_first, PIR_first, AFD_first),
                GRU_margin = GRU_first - max(SPD_first, FDP_first, CSU_first, CDU_first, LIN_first, NPD_first, PIR_first, AFD_first),
                CSU_margin = CSU_first - max(SPD_first, FDP_first, CDU_first, GRU_first, LIN_first, NPD_first, PIR_first, AFD_first),
                LIN_margin = LIN_first - max(SPD_first, FDP_first, CSU_first, GRU_first, CDU_first, NPD_first, PIR_first, AFD_first),
                NPD_margin = NPD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, CDU_first, PIR_first, AFD_first),
                AFD_margin = AFD_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, CDU_first, PIR_first, NPD_first),
                PIR_margin = PIR_first - max(SPD_first, FDP_first, CSU_first, GRU_first, LIN_first, CDU_first, NPD_first, AFD_first)) %>%
  pivot_longer(cols = c(CDU_margin, SPD_margin, FDP_margin, CSU_margin, GRU_margin, LIN_margin, NPD_margin, PIR_margin, AFD_margin), 
               names_to = "party", values_to = "margin") %>%
  dplyr::mutate(party = gsub("_margin", "", party)) %>%
  dplyr::filter(CSU_first != 0 | party != "CSU")%>%
  dplyr::filter(CDU_first != 0 | party != "CDU")%>%
  dplyr::filter(SPD_first != 0 | party != "SPD")%>%
  dplyr::filter(FDP_first != 0 | party != "FDP")%>%
  dplyr::filter(GRU_first != 0 | party != "GRU")%>%
  dplyr::filter(LIN_first != 0 | party != "LIN")%>%
  dplyr::filter(NPD_first != 0 | party != "NPD")%>%
  dplyr::filter(PIR_first != 0 | party != "PIR")%>%
  dplyr::filter(AFD_first != 0 | party != "AFD")


outcome21 <- read_excel("btw2021_kerg2.xlsx", 
                        skip = 10) %>%
  dplyr::filter(Gebietsart == "Wahlkreis" & Stimme == 1 & 
                  Gruppenart %in% c("Einzelbewerber/Wählergruppe","Partei") & 
                  !is.na(Anzahl)) %>%
  dplyr::mutate(Gebietsnummer = as.numeric(Gebietsnummer)) %>%
  group_by(Gebietsnummer) %>% slice(which.max(Anzahl)) %>%
  dplyr::mutate(Gruppenname = recode(Gruppenname, "GRÜNE" = "GRU", "DIE LINKE" = "LIN", "AfD" = "AFD"))

fptp_results17$winner_2021 <- NA
for (i in unique(fptp_results17$`Wkr-Nr.`)) {
  fptp_results17$winner_2021[fptp_results17$`Wkr-Nr.` == i] <- 
    outcome21$Gruppenname[outcome21$Gebietsnummer == i]
}

fptp_results17$win_t_plus_1 <- ifelse(fptp_results17$party == fptp_results17$winner_2021,1,0)

#### Complete FPTP Dataset ####

fptp_data <- rbind.data.frame(
  fptp_results72 %>% dplyr::select(Land, party, WahlkreisNr., margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = WahlkreisNr.) %>% mutate(year = 1972),
  fptp_results76 %>% dplyr::select(Land, party, wk_1980, margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = wk_1980) %>% mutate(year = 1976),
  fptp_results80 %>% dplyr::select(Land, party, WahlkreisNr., margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = WahlkreisNr.) %>% mutate(year = 1980),
  fptp_results83 %>% dplyr::select(Land, party, WahlkreisNr., margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = WahlkreisNr.) %>% mutate(year = 1983),
  fptp_results87 %>% dplyr::select(Land, party, WahlkreisNr., margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = WahlkreisNr.) %>% mutate(year = 1987),
  fptp_results90 %>% dplyr::select(Land, party, WahlkreisNr., margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = WahlkreisNr.) %>% mutate(year = 1990),
  fptp_results94 %>% dplyr::rename(Land = Ld.) %>% 
    dplyr::rename(wkr_t_plus_1 = Wkr) %>%
    dplyr::mutate(Land = recode(as.character(Land), "1" = "SH", "2" = "HH",
                                "3" = "NI", "4" = "HB", "5" = "NW", "6" = "HE",
                                "7" = "RP", "8" = "BW", "9" = "BY", "10" = "SL",
                                "11" = "BE", "12" = "MV", "13" = "BB", "14" = "ST",
                                "15" = "TH", "16" = "SN")) %>%
    dplyr::select(Land, party, wkr_t_plus_1, margin, win_t_plus_1) %>% mutate(year = 1994),
  fptp_results98 %>% dplyr::select(Land, party, `Wkr-Nr.`, margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = `Wkr-Nr.`) %>% mutate(year = 1998),
  fptp_results02 %>% dplyr::select(Land, party, `Wkr-Nr.`, margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = `Wkr-Nr.`) %>% mutate(year = 2002) %>%
    dplyr::mutate(Land = recode(Land, "Schleswig-Holstein" = "SH", "Hamburg" = "HH",
                                "Niedersachsen" = "NI", "Bremen" = "HB", "Nordrhein-Westfalen" = "NW", 
                                "Hessen" = "HE", "Rheinland-Pfalz" = "RP", "Baden-WŸrttemberg" = "BW", 
                                "Bayern" = "BY", "Saarland" = "SL", "Berlin" = "BE", 
                                "Mecklenburg-Vorpommern" = "MV", "Brandenburg" = "BB", 
                                "Sachsen-Anhalt" = "ST","ThŸringen" = "TH", "Sachsen" = "SN")),
  fptp_results05 %>% dplyr::select(Land, party, `Wkr.-Nr.`, margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = `Wkr.-Nr.`) %>% mutate(year = 2005) %>%
    dplyr::mutate(Land = recode(Land, "Schleswig-Holstein" = "SH", "Hamburg" = "HH",
                                "Niedersachsen" = "NI", "Bremen" = "HB", "Nordrhein-Westfalen" = "NW", 
                                "Hessen" = "HE", "Rheinland-Pfalz" = "RP", "Baden-WŸrttemberg" = "BW", 
                                "Bayern" = "BY", "Saarland" = "SL", "Berlin" = "BE", 
                                "Mecklenburg-Vorpommern" = "MV", "Brandenburg" = "BB", 
                                "Sachsen-Anhalt" = "ST","ThŸringen" = "TH", "Sachsen" = "SN")),
  fptp_results09 %>% dplyr::select(Land, party, `Wkr.-Nr.`, margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = `Wkr.-Nr.`) %>% mutate(year = 2009),
  fptp_results13 %>% dplyr::select(Land, party, `Wkr-Nr.`, margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = `Wkr-Nr.`) %>% mutate(year = 2013),
  fptp_results17 %>% dplyr::select(Land, party, `Wkr-Nr.`, margin, win_t_plus_1) %>% 
    dplyr::rename(wkr_t_plus_1 = `Wkr-Nr.`) %>% mutate(year = 2017))

#### Model Prospective Win Probability in FPTP ####

fptp_data$prob_win_t_plus_1 <- NA

# predict prob of winning in 1987 on the basis of 1983 margin
# plugged into a fit predicting probability of winning in 
# 1976-1980-1983 on the basis of margins in 1972-1976-1980

model1987 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1972, 1976, 1980)), 
                 win_t_plus_1 ~ margin*Land, family = "binomial")
probs1987 <- predict(model1987, newdata = fptp_data  %>% dplyr::filter(year == 1983) %>% 
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 1983] <- probs1987

# predict prob of winning in 1990 on the basis of 1987 margin
# plugged into a fit predicting probability of winning in 
# 1980-1983-1987 on the basis of margins in 1976-1980-1983

model1990 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1976, 1980, 1983)), 
                 win_t_plus_1 ~ margin*Land, family = "binomial")
probs1990 <- predict(model1990, newdata = fptp_data  %>% 
                       dplyr::filter(year == 1987) %>% 
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 1987] <- probs1990

# And so on...

model1994 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1980, 1983, 1987)), 
                 win_t_plus_1 ~ margin*Land, family = "binomial")
probs1994 <- predict(model1994, newdata = fptp_data  %>% 
                       dplyr::filter(year == 1990 & Land %notin% c("BB", "MV", "SN", "ST", "TH")) %>% 
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 1990 & 
                              fptp_data$Land %notin% c("BB", "MV", "SN", "ST", "TH")] <- probs1994

model1998 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1983, 1987, 1990)), 
                 win_t_plus_1 ~ margin*Land, family = "binomial")
probs1998 <- predict(model1998, newdata = fptp_data %>% dplyr::filter(year == 1994 & Land %notin% c("BE")) %>%
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 1994 & 
                              fptp_data$Land %notin% c("BE")] <- probs1998

model2002 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1987, 1990, 1994)), 
                 win_t_plus_1 ~ margin*Land, family = "binomial")
probs2002 <- predict(model2002, newdata = fptp_data %>% dplyr::filter(year == 1998) %>%
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 1998] <- probs2002

model2005 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1990, 1994, 1998)), win_t_plus_1 ~ margin*Land, 
                 family = "binomial")
probs2005 <- predict(model2005, newdata = fptp_data %>% dplyr::filter(year == 2002) %>%
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 2002] <- probs2005

model2009 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1994, 1998, 2002)), win_t_plus_1 ~ margin*Land, 
                 family = "binomial")
probs2009 <- predict(model2009, newdata = fptp_data %>% dplyr::filter(year == 2005) %>%
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 2005] <- probs2009

model2013 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(1998, 2002, 2005)), win_t_plus_1 ~ margin*Land, 
                 family = "binomial")
probs2013 <- predict(model2013, newdata = fptp_data %>% dplyr::filter(year == 2009) %>%
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 2009] <- probs2013

model2017 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(2002, 2005, 2009)), win_t_plus_1 ~ margin*Land, 
                 family = "binomial")
probs2017 <- predict(model2017, newdata = fptp_data %>% dplyr::filter(year == 2013) %>%
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 2013] <- probs2017

model2021 <- glm(data = fptp_data %>% dplyr::filter(year %in% c(2005, 2009, 2013)), win_t_plus_1 ~ margin*Land, 
                 family = "binomial")
probs2021 <- predict(model2021, newdata = fptp_data %>% dplyr::filter(year == 2017) %>%
                       dplyr::select(margin, Land), type = "response")

fptp_data$prob_win_t_plus_1[fptp_data$year == 2017] <- probs2021

plot(fptp_data$margin, fptp_data$prob_win_t_plus_1, cex = 0.1)

#### Get Closed-List PR results at t and t+1 ####

# 1976 election list results

# lowest list position elected via lists, for each party and state
last_list76 <- rio::import("btw1976bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

# district magnitude (number of MPs elected via lists by state)
magnitude76 <- rio::import("btw1976bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

# actual electoral results given list position
pr_results76 <- rio::import("btw1976bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`)

pr_results76 <- left_join(pr_results76, last_list76, by = c("party", "Land")) 

pr_results76$last_elected[is.na(pr_results76$last_elected)] <- 0

pr_results76 <- left_join( pr_results76 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), magnitude76, by = "Land")

# Repeat for other elections...
# 1980 election

last_list80 <- rio::import("btw1980_bewerb_anon.rds")  %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude80 <- rio::import("btw1980_bewerb_anon.rds")  %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results80 <- rio::import("btw1980_bewerb_anon.rds")  %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`)

pr_results80 <- left_join(pr_results80, last_list80, by = c("party", "Land")) 

pr_results80$last_elected[is.na(pr_results80$last_elected)] <- 0

pr_results80 <- left_join( pr_results80 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude80, by = "Land")

table(pr_results80$eligible, pr_results80$`Gewählt\r\n1=über Wkr\r\n2=über LL`)

# 1983 election

last_list83 <- rio::import("btw1983bewerb_anon.rds")%>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude83 <- rio::import("btw1983bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results83 <- rio::import("btw1983bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`)

pr_results83 <- left_join(pr_results83, last_list83, by = c("party", "Land")) 

pr_results83$last_elected[is.na(pr_results83$last_elected)] <- 0

pr_results83 <- left_join( pr_results83 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude83, by = "Land")

table(pr_results83$eligible, pr_results83$`Gewählt\r\n1=über Wkr\r\n2=über LL`)

# 1987 election

last_list87 <- rio::import("btw1987bewerb_anon.rds")%>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude87 <- rio::import("btw1987bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results87 <- rio::import("btw1987bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`)

pr_results87 <- left_join(pr_results87, last_list87, by = c("party", "Land")) 

pr_results87$last_elected[is.na(pr_results87$last_elected)] <- 0

pr_results87 <- left_join( pr_results87 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude87, by = "Land")

table(pr_results87$eligible, pr_results87$`Gewählt\r\n1=über Wkr\r\n2=über LL`)

# 1990 election

last_list90 <- rio::import("btw1990bewerb_anon.rds")%>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude90 <- rio::import("btw1990bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results90 <- rio::import("btw1990bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei/\r\nListenvereinigung`)

pr_results90 <- left_join(pr_results90, last_list90, by = c("party", "Land")) 

pr_results90$last_elected[is.na(pr_results90$last_elected)] <- 0

pr_results90 <- left_join( pr_results90 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude90, by = "Land")

table(pr_results90$eligible, pr_results90$`Gewählt\r\n1=über Wkr\r\n2=über LL`)


# 1994 election

last_list94 <- rio::import("btw1994bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude94 <- rio::import("btw1994bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results94 <- rio::import("btw1994bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei`)

pr_results94 <- left_join(pr_results94, last_list94, by = c("party", "Land")) 

pr_results94$last_elected[is.na(pr_results94$last_elected)] <- 0

pr_results94 <- left_join( pr_results94 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude94, by = "Land")

table(pr_results94$eligible, pr_results94$`Gewählt\r\n1=über Wkr\r\n2=über LL`)

# 1998 election

last_list98 <- rio::import("btw1998bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude98 <- rio::import("btw1998bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results98 <- rio::import("btw1998bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei`)

pr_results98 <- left_join(pr_results98, last_list98, by = c("party", "Land")) 

pr_results98$last_elected[is.na(pr_results98$last_elected)] <- 0

pr_results98 <- left_join( pr_results98 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude98, by = "Land")

table(pr_results98$eligible, pr_results98$`Gewählt\r\n1=über Wkr\r\n2=über LL`)

# 2002 election

last_list02 <- rio::import("btw2002bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::rename(party = `Partei`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude02 <- rio::import("btw2002bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt\r\n1=über Wkr\r\n2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results02 <- rio::import("btw2002bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei`)

pr_results02 <- left_join(pr_results02, last_list02, by = c("party", "Land")) 

pr_results02$last_elected[is.na(pr_results02$last_elected)] <- 0

pr_results02 <- left_join( pr_results02 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude02, by = "Land")

table(pr_results02$eligible, pr_results02$`Gewählt\r\n1=über Wkr\r\n2=über LL`)

# 2005 election

last_list05 <- rio::import("btw2005bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 2) %>%
  dplyr::rename(party = `Partei`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude05 <- rio::import("btw2005bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results05 <- rio::import("btw2005bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei`)

pr_results05 <- left_join(pr_results05, last_list05, by = c("party", "Land")) 

pr_results05$last_elected[is.na(pr_results05$last_elected)] <- 0

pr_results05 <- left_join( pr_results05 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude05, by = "Land")

table(pr_results05$eligible, pr_results05$`Gewählt: 1=über Wkr; 2=über LL`)

# 2009 election

last_list09 <- rio::import("btw2009bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 2) %>%
  dplyr::rename(party = `Partei`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude09 <- rio::import("btw2009bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results09 <- rio::import("btw2009bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei`)

pr_results09 <- left_join(pr_results09, last_list09, by = c("party", "Land")) 

pr_results09$last_elected[is.na(pr_results09$last_elected)] <- 0

pr_results09 <- left_join( pr_results09 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude09, by = "Land")

table(pr_results09$eligible, pr_results09$`Gewählt: 1=über Wkr; 2=über LL`)


# 2013 election

last_list13 <- rio::import("btw2013bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 2) %>%
  dplyr::rename(party = `Partei`) %>%
  dplyr::group_by(Land, party) %>%
  dplyr::summarise(last_elected = max(Platz))

magnitude13 <- rio::import("btw2013bewerb_anon.rds") %>%
  dplyr::filter(`Gewählt: 1=über Wkr; 2=über LL` == 2) %>%
  dplyr::group_by(Land) %>%
  dplyr::summarise(magnitude = n())

pr_results13 <- rio::import("btw2013bewerb_anon.rds") %>%
  dplyr::filter(!is.na(Platz)) %>%
  dplyr::rename(party = `Partei`)

pr_results13 <- left_join(pr_results13, last_list13, by = c("party", "Land")) 

pr_results13$last_elected[is.na(pr_results13$last_elected)] <- 0

pr_results13 <- left_join( pr_results13 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude13, by = "Land")

table(pr_results13$eligible, pr_results13$`Gewählt: 1=über Wkr; 2=über LL`)


# 2017 election

last_list17 <- rio::import("btw2017bewerb_1_anon.rds") %>%
  dplyr::filter(elected_on_list == 1) %>%
  dplyr::group_by(state, party) %>%
  dplyr::summarise(last_elected = max(Liste_Platz)) %>%
  dplyr::rename(Land = state)

magnitude17 <- rio::import("btw2017bewerb_1_anon.rds") %>%
  dplyr::filter(elected_on_list == 1) %>%
  dplyr::group_by(state) %>%
  dplyr::summarise(magnitude = n()) %>%
  dplyr::rename(Land = state)

pr_results17 <- rio::import("btw2017bewerb_2_anon.rds") %>%
  dplyr::filter(!is.na(Liste_Platz)) %>%
  dplyr::rename(party = `Liste_ParteiKurzBez`, Platz = `Liste_Platz`, Land = Liste_Land) %>%
  dplyr::mutate(party = recode(party, "DIE LINKE" = "LINKE/PDS",
                               "GRÜNE" = "Grüne / B90 / AL / Neues Forum"))

pr_results17 <- left_join(pr_results17, last_list17, by = c("party", "Land")) 

pr_results17$last_elected[is.na(pr_results17$last_elected)] <- 0

pr_results17 <- left_join( pr_results17 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude17, by = "Land")


# 2021 election

last_list21 <- rio::import("btw2021bewerb_1_anon.rds") %>%
  dplyr::filter(elected_on_list == 1) %>%
  dplyr::group_by(state, party) %>%
  dplyr::summarise(last_elected = max(Listenplatz)) %>%
  dplyr::rename(Land = state)

magnitude21 <- rio::import("btw2021bewerb_1_anon.rds") %>%
  dplyr::filter(elected_on_list == 1) %>%
  dplyr::group_by(state) %>%
  dplyr::summarise(magnitude = n()) %>%
  dplyr::rename(Land = state)

pr_results21 <- rio::import("btw2021bewerb_2_anon.rds") %>%
  dplyr::filter(Listenplatz != 0 & Gebietsart == "Land") %>%
  dplyr::rename(party = `Gruppenname`, Platz = `Listenplatz`, Land = GebietLandAbk) %>%
  dplyr::mutate(party = recode(party, "DIE LINKE" = "LINKE/PDS",
                               "GRÜNE" = "Grüne / B90 / AL / Neues Forum"))

pr_results21 <- left_join(pr_results21, last_list21, by = c("party", "Land")) 

pr_results21$last_elected[is.na(pr_results21$last_elected)] <- 0

pr_results21 <- left_join( pr_results21 %>%
                             dplyr::mutate(eligible = ifelse(Platz <= last_elected, 1, 0)), 
                           magnitude21, by = "Land")


#### Full PR dataset ####

pr_data <- rbind.data.frame(
  
  pr_results76 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "F.D.P.", "FDP/DPS", "FDP/DVP", "SPD", "NPD")) %>%
    dplyr::mutate(party = recode(party, "F.D.P." = "FDP",
                                 "FDP/DPS" = "FDP", 
                                 "FDP/DVP" = "FDP"), year = 1976) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results80 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "F.D.P.", "SPD", "NPD", "GRUENE"))%>%
    dplyr::mutate(party = recode(party, "F.D.P." = "FDP",
                                 "GRUENE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD"), year = 1980) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results83 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "F.D.P.", "SPD", "NPD", "GRUENE"))%>%
    dplyr::mutate(party = recode(party, "F.D.P." = "FDP",
                                 "GRUENE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD"), year = 1983) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results87 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "F.D.P.", "SPD", "NPD", "GRUENE"))%>%
    dplyr::mutate(party = recode(party, "F.D.P." = "FDP",
                                 "GRUENE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD"), year = 1987) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results90 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "F.D.P.", "SPD", "NPD", "B90/Grüne", 
                               "GRÜNE", "PDS", "REP"))%>%
    dplyr::mutate(party = recode(party, "F.D.P." = "FDP",
                                 "B90/Grüne" = "Grüne / B90 / AL / Neues Forum",
                                 "GRÜNE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD",
                                 "PDS" = "LINKE/PDS"), year = 1990) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results94 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "F.D.P.", "SPD", "GRÜNE", 
                               "PDS", "REP", "STATT Partei"))%>%
    dplyr::mutate(party = recode(party, "F.D.P." = "FDP",
                                 "GRÜNE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD",
                                 "PDS" = "LINKE/PDS",
                                 "STATT Partei" = "STATT"), year = 1994) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results98 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "F.D.P.", "SPD", "GRÜNE", 
                               "PDS", "REP", "DVU", "NPD"))%>%
    dplyr::mutate(party = recode(party, "F.D.P." = "FDP",
                                 "GRÜNE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD",
                                 "DVU" = "DVU/NPD",
                                 "PDS" = "LINKE/PDS"), year = 1998) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results02 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "FDP", "SPD", "GRÜNE", 
                               "PDS", "REP", "DVU", "NPD"))%>%
    dplyr::mutate(party = recode(party, "GRÜNE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD",
                                 "DVU" = "DVU/NPD",
                                 "PDS" = "LINKE/PDS"), year = 2002) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results05 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "FDP", "SPD", "GRÜNE", 
                               "Die Linke.", "REP", "NPD"))%>%
    dplyr::mutate(party = recode(party, "GRÜNE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD",
                                 "Die Linke." = "LINKE/PDS"), year = 2005) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results09 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "FDP", "SPD", "GRÜNE", 
                               "DIE LINKE", "REP", "NPD", "DVU", "PIRATEN"))%>%
    dplyr::mutate(party = recode(party, "GRÜNE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD",
                                 "DVU" = "DVU/NPD",
                                 "DIE LINKE" = "LINKE/PDS"), year = 2009) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results13 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "FDP", "SPD", "GRÜNE", "AfD", "FREIE WÄHLER",
                               "DIE LINKE", "REP", "NPD", "PIRATEN"))%>%
    dplyr::mutate(party = recode(party, "GRÜNE" = "Grüne / B90 / AL / Neues Forum",
                                 "NPD" = "DVU/NPD",
                                 "DIE LINKE" = "LINKE/PDS"), year = 2013) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results17 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "FDP", "SPD", "Grüne / B90 / AL / Neues Forum", 
                               "AfD", "FREIE WÄHLER",
                               "LINKE/PDS", "REP", "NPD", "PIRATEN"))%>%
    dplyr::mutate(party = recode(party,"NPD" = "DVU/NPD"), year = 2017) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year),
  
  pr_results21 %>% 
    dplyr::filter(party %in% c("CDU", "CSU", "FDP", "SPD", "Grüne / B90 / AL / Neues Forum", 
                               "AfD", "FREIE WÄHLER", "SSW",
                               "LINKE/PDS", "REP", "NPD", "PIRATEN"))%>%
    dplyr::mutate(party = recode(party,"NPD" = "DVU/NPD"), year = 2021) %>%
    dplyr::rename(position = Platz, state = Land) %>%
    dplyr::select(position, party, state, magnitude, eligible, year)
  
)

pr_data$state_party <- paste(pr_data$state, pr_data$party)

pr_data$prob_win_t <- NA

# get fit for probability of being elected in 1976, 1980, 1983
# given list position in those elections, plug in list position in 1987
# to get prospective candidacy quality in the lists in that year

model1987pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(1976, 1980, 1983)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs1987pr <- predict.glm(model1987pr, newdata = pr_data %>% dplyr::filter(year %in% c(1987)), 
                           type = "response")

pr_data$prob_win_t[pr_data$year == 1987] <- probs1987pr

# Repeat for other elections...

model1990pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(1980, 1983, 1987)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs1990pr <- predict(model1990pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(1990) &
                                                     state %notin% c("BB", "BE", "MV", "SN", "ST", "TH") &
                                                     party %notin% c("LINKE/PDS", "REP")), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 1990 & 
                     pr_data$state %notin% c("BB", "BE", "MV", "SN", "ST", "TH") &
                     pr_data$party %notin% c("LINKE/PDS", "REP")] <- probs1990pr


model1994pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(1983, 1987, 1990)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs1994pr <- predict(model1994pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(1994) &
                                                     party %notin% c("STATT")), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 1994 & 
                     pr_data$party %notin% c("STATT")] <- probs1994pr


model1998pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(1987, 1990, 1994)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs1998pr <- predict(model1998pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(1998)), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 1998] <- probs1998pr



model2002pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(1990, 1994, 1998)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs2002pr <- predict(model2002pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(2002)), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 2002] <- probs2002pr


model2005pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(1994, 1998, 2002)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs2005pr <- predict(model2005pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(2005)), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 2005] <- probs2005pr


model2009pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(1998, 2002, 2005)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs2009pr <- predict(model2009pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(2009) & party %notin% c("PIRATEN")), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 2009 & pr_data$party %notin% c("PIRATEN")] <- probs2009pr


model2013pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(2002, 2005, 2009)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs2013pr <- predict(model2013pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(2013) & party %notin% c("AfD", "FREIE WÄHLER") & 
                                                     state_party %notin% c("SN PIRATEN")), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 2013 & pr_data$party %notin% c("AfD", "FREIE WÄHLER")& 
                     pr_data$state_party %notin% c("SN PIRATEN")] <- probs2013pr


model2017pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(2005, 2009, 2013)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs2017pr <- predict(model2017pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(2017)), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 2017] <- probs2017pr


model2021pr <- glm(data = pr_data %>% dplyr::filter(year %in% c(2009, 2013, 2017)),
                   eligible ~ state_party*position + magnitude*position, family = "binomial")

probs2021pr <- predict(model2021pr, newdata = 
                         pr_data %>% dplyr::filter(year %in% c(2021) & party %notin% c("SSW")), 
                       type = "response")

pr_data$prob_win_t[pr_data$year == 2021 & pr_data$party %notin% c("SSW")] <- probs2021pr

# PR: code last_elected at t-1

pr_data <- pr_data %>%
  dplyr::mutate(party = recode(party, "NPD" = "DVU/NPD"))

# year is t+1 to link with pr_data
last_list_combined <- rbind.data.frame(
  last_list76 %>% dplyr::mutate(year = 1980),
  last_list80 %>% dplyr::mutate(year = 1983),
  last_list83 %>% dplyr::mutate(year = 1987),
  last_list87 %>% dplyr::mutate(year = 1990),
  last_list90 %>% dplyr::mutate(year = 1994),
  last_list94 %>% dplyr::mutate(year = 1998),
  last_list98 %>% dplyr::mutate(year = 2002),
  last_list02 %>% dplyr::mutate(year = 2005),
  last_list05 %>% dplyr::mutate(year = 2009),
  last_list09 %>% dplyr::mutate(year = 2013),
  last_list13 %>% dplyr::mutate(year = 2017),
  last_list17 %>% dplyr::mutate(year = 2021)) %>% ungroup() %>%
  dplyr::mutate(party = recode(party, "B90/Grüne" = "Grüne / B90 / AL / Neues Forum",
                               "DIE LINKE" = "LINKE/PDS",
                               "Die Linke." = "LINKE/PDS",
                               "PDS" = "LINKE/PDS",
                               "F.D.P." = "FDP",
                               "FDP/DVP" = "FDP",
                               "GRUENE" = "Grüne / B90 / AL / Neues Forum",
                               "GRÜNE" = "Grüne / B90 / AL / Neues Forum")) %>%
  dplyr::rename(state = Land, last_elected_t_minus_1 = last_elected)

pr_data <- left_join(pr_data, last_list_combined, by = c("party", "state", "year"))

pr_data$last_elected_t_minus_1[is.na(pr_data$last_elected_t_minus_1)] <- 0 
pr_data$last_elected_t_minus_1[pr_data$state %in% c("BB", "BE", "MV", "SN", "ST", "TH") &
                                 pr_data$year == 1990] <- NA
pr_data$last_elected_t_minus_1[pr_data$year == 1976] <- NA

# save output (used in LSQ_mainanalysis file)

write_rds(fptp_data, "fptp_data_germany_27092023.rds")
write_rds(pr_data, "pr_data_germany_27092023.rds")





